﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace FySystem.Entity.Common
{
    /// <summary>
    /// 分页查询数据传入实体
    /// </summary>
    public class QueryITO
    {
        private static JsonSerializerOptions _jsopt = new JsonSerializerOptions()
        {
            Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
            NumberHandling= System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString
        };

        /// <summary>
        /// json字符串
        /// </summary>
        public string Conditions { get; set; }

        /// <summary>
        /// 当前页，由前端传入
        /// </summary>
        public int PageIndex {  get; set; }

        /// <summary>
        /// 每页显示数量，由前端传入
        /// </summary>
        public int Perpage { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        public string Sort { get; set; }

        /// <summary>
        /// 解析查询条件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public T GetConditions<T>()
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(Conditions) && Conditions!="{}")
                    return JsonSerializer.Deserialize<T>(Conditions, _jsopt);
                else
                    return default(T);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public string GetOrderByString()
        {
            StringBuilder res = new StringBuilder();
            try
            {
                if (!string.IsNullOrWhiteSpace(Sort))
                {
                    var orders = JsonSerializer.Deserialize<Dictionary<string, string>>(Sort, _jsopt);
                    bool isFirst = true;
                    foreach (var key in orders.Keys)
                    {
                        if (isFirst)
                        {
                            res.Append(key).Append(" ").Append(orders[key]);
                            isFirst = false;
                        }
                        else
                        {
                            res.Append(',').Append(key).Append(" ").Append(orders[key]);
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return res.ToString();
        }
    }

    public static class QueryExtension
    {
        /// <summary>
        /// 传入是否合法
        /// </summary>
        /// <param name="queryITO"></param>
        /// <returns></returns>
        public static bool IsNullOrInvalid(this QueryITO queryITO)
        {
            bool res = false;
            try
            {
                if (queryITO == null)
                    res = true;
                else if (queryITO.PageIndex <= 0)
                    res = true;
                else if (queryITO.Perpage <= 0)
                    res = true;
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }
    }
}
